package com.zmovie.admin.controller.movie;

import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.MovieArea;
import com.zmovie.framework.service.MovieAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 电影区域模块
 * @since 2023-11-30 16:00
 */
@Controller
@RequestMapping("/movie/area")
@Api(tags = "电影区域模块")
public class MovieAreaController {
    @Autowired
    private MovieAreaService movieAreaService;

    @GetMapping
    @ApiOperation("电影区域首页")
    public String index(Model model){
        List<MovieArea> movieAreas = movieAreaService.listMovieArea(new MovieArea());
        model.addAttribute(Constants.MOVIE_AREAS_KEY, movieAreas);
        return "movie/movie_area";
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取电影区域")
    @ResponseBody
    public Object getMovieArea(@PathVariable("id") @NotNull Integer id){
        return CommonReturnType.success(movieAreaService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加电影区域")
    @ResponseBody
    public Object addMovieArea(@Validated MovieArea movieArea) throws BusinessException {
        try {
            boolean save = movieAreaService.save(movieArea);
            if(!save){
                return CommonReturnType.fail("添加失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(EnumBusinessError.OPERATOR_ERROR);
        }
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ApiOperation("修改电影区域")
    @ResponseBody
    public Object updateMovieArea(@Validated MovieArea movieArea) throws BusinessException {
        //参数校验
        if(movieArea.getId() == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        movieAreaService.updateById(movieArea);
        return CommonReturnType.success("修改成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除电影区域")
    @ResponseBody
    public Object deleteMovieArea(@PathVariable("id") @NotNull Integer id){
        movieAreaService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除电影区域")
    @ResponseBody
    public Object deleteMovieAreaBatch(Integer[] id) throws BusinessException {
        //参数校验
        if(id == null || id.length == 0){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        movieAreaService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索电影区域")
    public Object search(MovieArea movieArea, Model model){
        List<MovieArea> movieAreas = movieAreaService.listMovieArea(movieArea);
        model.addAttribute(Constants.MOVIE_AREAS_KEY, movieAreas);

        return "movie/movie_area :: areaList";
    }
}
